package com.lanou.moving.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.adapter.SongListClassifyAdapter;
import com.lanou.moving.base.BaseActivity;
import com.lanou.moving.base.BaseApplication;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.SongListClassify;
import com.lanou.moving.fragment.SongListHotFragment;
import com.lanou.moving.fragment.SongListReplaceFragment;
import com.lanou.moving.view.MyGridView;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘琪 on 15/10/27.
 */
public class SongListActivity extends BaseActivity implements
        AdapterView.OnItemClickListener, RadioGroup.OnCheckedChangeListener,
        View.OnClickListener {

    @Bind(R.id.song_list_all_tv)
    TextView songListAllTv;
    @Bind(R.id.song_list_hot_rb)
    RadioButton songListHotRb;
    @Bind(R.id.song_list_recent_rb)
    RadioButton songListRecentRb;
    @Bind(R.id.song_list_rg)
    RadioGroup songListRg;
    @Bind(R.id.song_list_all)
    TextView songListAll;
    @Bind(R.id.song_list_tag_zero)
    TextView songListTagZero;
    @Bind(R.id.song_list_grid_zero)
    MyGridView songListGridZero;
    @Bind(R.id.song_list_tag_one)
    TextView songListTagOne;
    @Bind(R.id.song_list_grid_one)
    MyGridView songListGridOne;
    @Bind(R.id.song_list_tag_two)
    TextView songListTagTwo;
    @Bind(R.id.song_list_grid_two)
    MyGridView songListGridTwo;
    @Bind(R.id.song_list_tag_three)
    TextView songListTagThree;
    @Bind(R.id.song_list_grid_three)
    MyGridView songListGridThree;
    @Bind(R.id.song_list_tag_four)
    TextView songListTagFour;
    @Bind(R.id.song_list_grid_four)
    MyGridView songListGridFour;
    @Bind(R.id.song_list_linear)
    LinearLayout songListLinear;
    @Bind(R.id.song_list_relative)
    RelativeLayout songListRelative;

    private VolleySingleton volleySingleton;
    private StringRequestUtf request;
    private List<SongListClassify.DataEntity> classifies;
    private SongListClassifyAdapter adapter;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private SongListHotFragment songListHotFragment;
    private SongListReplaceFragment songListReplaceFragment;
    private String classify;// 最新最热分类
    private String list;
    // 箭头方向
    private Drawable toRight;
    private Drawable toBottom;

    private ImageView singer_back, search;
    private TextView singer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_list);
        BaseApplication.getInstance().addActivity(this);
        singer_back = (ImageView) findViewById(R.id.singer_back);
        singer_back.setOnClickListener(this);
        search = (ImageView) findViewById(R.id.search);
        search.setOnClickListener(this);
        singer = (TextView) findViewById(R.id.singer);
        singer.setText("歌单");

        // 初始化fragment
        songListHotFragment = new SongListHotFragment();
        songListReplaceFragment = new SongListReplaceFragment();

        volleySingleton = VolleySingleton.getInstance(this);
        ButterKnife.bind(this);
        songListRg.setOnCheckedChangeListener(this);
        songListAllTv.setOnClickListener(this);
        toBottom = getResources().getDrawable(R.mipmap.arrow_to_bottom);
        toBottom.setBounds(0, 0, toBottom.getMinimumWidth(), toBottom.getMinimumHeight());
        toRight = getResources().getDrawable(R.mipmap.arrow_to_right);
        toRight.setBounds(0, 0, toRight.getMinimumWidth(), toRight.getMinimumHeight());
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.song_list_relative, SongListReplaceFragment.newInstance(URL.SONG_LIST_HOT_START, URL.SONG_LIST_HOT_END));
        transaction.commit();
        getData();
    }

    private void getData() {
        songListGridZero.setOnItemClickListener(this);
        songListGridOne.setOnItemClickListener(this);
        songListGridTwo.setOnItemClickListener(this);
        songListGridThree.setOnItemClickListener(this);
        songListGridFour.setOnItemClickListener(this);
        songListAll.setOnClickListener(this);
        String url = URL.SONG_LIST;
        request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongListClassify classify = gson.fromJson(response, SongListClassify.class);
                classifies = classify.getData();

                adapter = new SongListClassifyAdapter(SongListActivity.this, classifies.get(0).getChildren());
                songListGridZero.setAdapter(adapter);

                adapter = new SongListClassifyAdapter(SongListActivity.this, classifies.get(1).getChildren());
                songListGridOne.setAdapter(adapter);

                adapter = new SongListClassifyAdapter(SongListActivity.this, classifies.get(2).getChildren());
                songListGridTwo.setAdapter(adapter);

                adapter = new SongListClassifyAdapter(SongListActivity.this, classifies.get(3).getChildren());
                songListGridThree.setAdapter(adapter);

                adapter = new SongListClassifyAdapter(SongListActivity.this, classifies.get(4).getChildren());
                songListGridFour.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SongListClassify.DataEntity.ChildrenEntity childrenEntity =
                (SongListClassify.DataEntity.ChildrenEntity) parent.getItemAtPosition(position);
        String tag = childrenEntity.getTag();
        try {
            list = URLEncoder.encode(tag, "utf-8");
            if (songListHotRb.isChecked() == true) {
                classify = URLEncoder.encode("最热", "utf-8");
                transaction = manager.beginTransaction();
                transaction.replace(R.id.song_list_relative, SongListReplaceFragment.
                        newInstance(URL.SONG_LIST_START, URL.SONG_LIST_AFTER_PAGE + classify + URL.SONG_LIST_AFTER_TYPE + list + URL.SONG_LIST_END));
                transaction.commit();
                songListLinear.setVisibility(View.GONE);
                songListAllTv.setCompoundDrawables(null, null, toRight, null);
                songListAllTv.setCompoundDrawablePadding(3);
                songListAllTv.setText(tag);
            } else {
                classify = URLEncoder.encode("最新", "utf-8");
                songListLinear.setVisibility(View.GONE);
                songListAllTv.setCompoundDrawables(null, null, toRight, null);
                songListAllTv.setCompoundDrawablePadding(3);
                songListAllTv.setText(tag);
                transaction = manager.beginTransaction();
                transaction.replace(R.id.song_list_relative, SongListReplaceFragment.
                        newInstance(URL.SONG_LIST_START, URL.SONG_LIST_AFTER_PAGE + classify + URL.SONG_LIST_AFTER_TYPE + list + URL.SONG_LIST_END));
                transaction.commit();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String text = (String) songListAllTv.getText();
        try {
            list = URLEncoder.encode(text, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        switch (checkedId) {
            case R.id.song_list_hot_rb:
                try {
                    classify = URLEncoder.encode("最热", "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (text.equals("全部")) {
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_list_relative, SongListReplaceFragment.
                            newInstance(URL.SONG_LIST_HOT_START, URL.SONG_LIST_HOT_END));
                    transaction.commit();
                } else {
                    String url_start = URL.SONG_LIST_START;
                    String url_end = URL.SONG_LIST_AFTER_PAGE + classify
                            + URL.SONG_LIST_AFTER_TYPE + list + URL.SONG_LIST_END;
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_list_relative, SongListReplaceFragment.
                            newInstance(url_start, url_end));
                    transaction.commit();
                }
                break;
            case R.id.song_list_recent_rb:
                try {
                    classify = URLEncoder.encode("最新", "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (text.equals("全部")) {
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_list_relative, SongListReplaceFragment.
                            newInstance(URL.SONG_LIST_RECENT_START, URL.SONG_LIST_RECENT_END));
                    transaction.commit();
                } else {
                    String url_start = URL.SONG_LIST_START;
                    String url_end = URL.SONG_LIST_AFTER_PAGE + classify
                            + URL.SONG_LIST_AFTER_TYPE + list + URL.SONG_LIST_END;
                    transaction = manager.beginTransaction();
                    transaction.replace(R.id.song_list_relative, SongListReplaceFragment.
                            newInstance(url_start, url_end));
                    transaction.commit();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.song_list_all_tv:
                if (songListLinear.getVisibility() == View.GONE) {
                    songListTagZero.setText(classifies.get(0).getTag());
                    songListTagOne.setText(classifies.get(1).getTag());
                    songListTagTwo.setText(classifies.get(2).getTag());
                    songListTagThree.setText(classifies.get(3).getTag());
                    songListTagFour.setText(classifies.get(4).getTag());
                    songListLinear.setVisibility(View.VISIBLE);
                    songListAllTv.setCompoundDrawables(null, null, toRight, null);
                    songListAllTv.setCompoundDrawablePadding(3);
                } else {
                    songListLinear.setVisibility(View.GONE);
                    songListAllTv.setCompoundDrawables(null, null, toBottom, null);
                    songListAllTv.setCompoundDrawablePadding(3);
                }
                break;
            case R.id.song_list_all:
                songListLinear.setVisibility(View.GONE);
                songListAllTv.setCompoundDrawables(null, null, toRight, null);
                songListAllTv.setCompoundDrawablePadding(3);
                songListAllTv.setText("全部");
                break;
            case R.id.singer_back:
                finish();
                break;
            case R.id.search:
                Intent intent = new Intent(SongListActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
        }
    }
}
